package com.bjpowernode.manager;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisManager {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void setValue(String key, Object value, long timeOut, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeOut, unit);
    }

    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public Boolean removeValue(String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 刷新key的过期时间
     *
     * @param key
     * @param timeOut
     * @param unit
     * @return
     */
    public Boolean flushKeyExpireTime(String key, long timeOut, TimeUnit unit) {
        return redisTemplate.expire(key, timeOut, unit);
    }

    public Long generateOnlyNumber(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

}
